﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Common;

namespace CIT.WMS.Msd
{
    public partial class MSD_IN_Baking : Form
    {
        DataTable dtMsd = new DataTable();

        public MSD_IN_Baking()
        {
            InitializeComponent();
        }

        private void txtSerialNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(txtSerialNumber.Text.Trim()))
                {
                    PublicUtils.ShowMsg(this.lab_msg, "ReelID不能为空", false);
                    return;
                }
                dtMsd = MSDService.Query(string.Format(" AND a.SerialNumber='{0}'", txtSerialNumber.Text.Trim()));
                if (dtMsd.Rows.Count == 0)//校验库存表是否存在该条码
                {
                    PublicUtils.ShowMsg(this.lab_msg, "ReelID错误", false);
                    txtSerialNumber.SelectAll();
                    txtSerialNumber.Focus();
                    return;
                }
                txtNumber.Focus();
            }
        }

        private bool ValidateInput(out string msg)
        {
            msg = "OK";
            if (string.IsNullOrEmpty(txtSerialNumber.Text.Trim()))
            {
                msg = "ReelID不能为空";
                return false;
            }
            if (string.IsNullOrEmpty(txtNumber.Text.Trim()))
            {
                msg = "序号不能为空";
                return false;
            }
            dtMsd = MSDService.Query(string.Format(" AND a.SerialNumber='{0}'", txtSerialNumber.Text.Trim()));
            if (dtMsd.Rows.Count == 0)//校验库存表是否存在该条码
            {
                PublicUtils.ShowMsg(this.lab_msg, "ReelID错误", false);
                txtSerialNumber.SelectAll();
                txtSerialNumber.Focus();
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["IsMSD"]) != "0")//校验是否是MSD
            {
                PublicUtils.ShowMsg(this.lab_msg, "ReelID非MSD", false);
                return false;
            }
            if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "5")
            {
                msg = "SerialNumber请勿重复入烘烤";
                return false;
            }
            //1.序号校验
            DataRow[] drs = dtMsd.Select(string.Format("OrderNum='{0}'", txtNumber.Text.Trim()));
            if (drs.Length == 0)
            {
                msg = "序号错误";
                return false;
            }
            //2.过期的物料不允许入烘烤箱  ( 出库/正常的状态则正确)
            if (Common.ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Lock_Flag"]) != "0" && Common.ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Lock_Flag"]) != "7")//目前只考虑正常状态 可以进行如烘烤
            {
                msg = "状态错误";
                return false;
            }
            //3.出干燥箱、拆封 、暴露超时允许进入烘烤箱 
            if (Common.ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "1" || Common.ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "4" || Common.ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) == "7")
            {
                MSDService.IN_Baking(txtSerialNumber.Text.Trim(), txtNumber.Text.Trim());
                return true;
            }
            else
            {
                msg = "当前状态不允许进入烘烤箱";
                return false;
            }
        }

        private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string msg = string.Empty;
                if (!ValidateInput(out msg))
                {
                    PublicUtils.ShowMsg(this.lab_msg, msg, false);
                    if (msg.Contains("序号"))
                    {
                        txtNumber.SelectAll();
                        txtNumber.Focus();
                    }
                    else
                    {
                        txtSerialNumber.SelectAll();
                        txtSerialNumber.Focus();
                    }
                    return;
                }
                PublicUtils.ShowMsg(this.lab_msg, "入烘烤成功", true);
                PublicUtils.WriteLog(txtSerialNumber.Text.Trim(), "入烘烤", ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["MaterialCode"]), ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["QTY"], 0));
                txtSerialNumber.Text = string.Empty;
                txtNumber.Text = string.Empty;
                txtSerialNumber.Focus();
            }
        }
    }
}